package com.fivefu.core.skyeye.event.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fivefu.base.common.utils.date.DateUtils;
import com.fivefu.base.common.utils.date.LocalDateTimeUtils;
import com.fivefu.base.common.utils.str.StrUtils;
import io.swagger.annotations.ApiModelProperty;

import java.lang.reflect.Field;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Pattern;

public class VerifyDataUtils {

    /**
     * 验证必填字段
     * @param entity
     * @return
     * @throws IllegalAccessException
     */
    public static String verifyRequiredColumn(Object entity) throws IllegalAccessException {
        Field[] fields = entity.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(ApiModelProperty.class)) {
                ApiModelProperty property = field.getAnnotation(ApiModelProperty.class);
                if (property.required()) {
                    field.setAccessible(true);
                    Object obj = field.get(entity);
                    if (obj == null  || (field.getType().equals(String.class) && StrUtils.isNull((String) obj))) {
                        if (StrUtils.isNotNull(property.value())) {
                            return property.value();
                        }else{
                            return field.getName();
                        }
                    }
                }
            }
        }
        return null;
    }

    /**
     * 验证JSON数据格式
     * @param value
     * @return
     */
    public static Object verifyJSONArray(String value){
        if (value.startsWith("{") && value.endsWith("}")){
            Map<String,Object> map = new HashMap<>();
            JSONObject object = JSON.parseObject(value);
            Iterator<String> it = object.keySet().iterator();
            while(it.hasNext()){
                String key = it.next();
                Object val = verifyJSONArray(object.getString(key));
                map.put(key,val);
            }
            return map;
        } else if (value.startsWith("[") && value.endsWith("]")){
            JSONArray array = JSON.parseArray(value);
            Object[] objectArr = new Object[array.size()];
            for (int i = 0 ; i < array.size() ; i ++){
                Object arr = verifyJSONArray(array.get(i).toString());
                objectArr[i] = arr;
            }
            return objectArr;
        }
        return value;
    }

    /**
     * 验证时间格式
     * @param time 时间
     * @param format 时间格式
     * @return
     */
    public static LocalDateTime verifyDateTime(String time , String format) {
        LocalDateTime dateTime = LocalDateTimeUtils.parse(time, format);
        return dateTime;
    }

    /**
     * 验证是否数字
     * 整数,小数,浮点数,负数
     * @param numStr 字符串
     * @return
     */
    public static boolean verifyNumber(String numStr){
        Pattern pattern = Pattern.compile("[\\-0-9][0-9]*(.[0-9]+)?");
        boolean flag = pattern.matcher(numStr).matches();
        return flag;
    }


    public static void main(String[] args) {
        String time = "0900";
        LocalDateTime time1 = DateUtils.parse(time, "HH:mm");
//        System.out.println(time1);

//        LocalDateTime time1 = LocalDateTimeUtils.parse(time, "HHmm");
        System.out.println(time1);
    }


}
